package com.hsyt.pfc.exception;


/**
 * @Description: Med运行时异常
 * @ClassName: GlobalRuntimeException
 * @Project: idea_project
 * @Package: com.hsyt.pfc.exception
 * @Author: wangze@hsyuntai.com
 * @Date: 2015 年 11 月 06 日 22:33
 * @Copyright: 版权归 Hundsun 所有
 * <ModifyLog>
 * @ModifyContent:
 * @Author:
 * @Date: </ModifyLog>
 */
public class GlobalRuntimeException extends RuntimeException implements IGlobalException {

    private static final long serialVersionUID = -2072494384408945789L;
    private String errorCode;
    private String msg;
    private Throwable throwable;

    public GlobalRuntimeException() {
        this(ExceptionEnums.SYSTEM_ERROR.getCode());
    }

    public GlobalRuntimeException(Throwable cause) {
        this(ExceptionEnums.SYSTEM_ERROR.getCode(),cause);
    }

    protected GlobalRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.throwable = cause;
    }

    public GlobalRuntimeException(String errorCode) {
        super(errorCode + ":" + CodeContainer.getCodeMessage(errorCode, new String[0]));
        this.errorCode = errorCode;
        this.msg = CodeContainer.getCodeMessage(errorCode, new String[0]);
    }

    public GlobalRuntimeException(String errorCode, Throwable e) {
        super(errorCode + ":" + CodeContainer.getCodeMessage(errorCode, new String[0]), e);
        this.errorCode = errorCode;
        this.throwable = e;
        this.msg = CodeContainer.getCodeMessage(errorCode, new String[0]);
    }

    public GlobalRuntimeException(String errorCode, Object... errorMessageParams) {
        super(errorCode + ":" + CodeContainer.getCodeMessage(errorCode, errorMessageParams));
        this.errorCode = errorCode;
        this.msg = CodeContainer.getCodeMessage(errorCode, errorMessageParams);
    }

    public GlobalRuntimeException(String errorCode, Object[] errorMessageParams, Throwable e) {
        super(errorCode + ":" + CodeContainer.getCodeMessage(errorCode, errorMessageParams), e);
        this.errorCode = errorCode;
        this.throwable = e;
        this.msg = CodeContainer.getCodeMessage(errorCode, errorMessageParams);
    }

    public GlobalRuntimeException(ExceptionEnums exceptionEnums, Throwable e) {
        super(exceptionEnums.getCode() + ":" + CodeContainer.getCodeMessage(exceptionEnums.getCode(), new String[0]), e);
        this.errorCode = exceptionEnums.getCode();
        this.throwable = e;
        this.msg = CodeContainer.getCodeMessage(exceptionEnums.getCode(), new String[0]);
    }

    public GlobalRuntimeException(ExceptionEnums exceptionEnums, Object... errorMessageParams) {
        super(exceptionEnums.getCode() + ":" + CodeContainer.getCodeMessage(exceptionEnums.getCode(), errorMessageParams));
        this.errorCode = exceptionEnums.getCode();
        this.msg = CodeContainer.getCodeMessage(exceptionEnums.getCode(), errorMessageParams);
    }

    public GlobalRuntimeException(ExceptionEnums exceptionEnums, Object[] errorMessageParams, Throwable e) {
        super(exceptionEnums.getCode() + ":" + CodeContainer.getCodeMessage(exceptionEnums.getCode(), errorMessageParams), e);
        this.errorCode = exceptionEnums.getCode();
        this.throwable = e;
        this.msg = CodeContainer.getCodeMessage(exceptionEnums.getCode(), errorMessageParams);
    }

    @Override
    public String getErrorCode() {
        return this.errorCode;
    }

    @Override
    public String getMsg() {
        return this.msg;
    }

    @Override
    public Throwable getCaluse() {
        return this.throwable;
    }

    @Override
    public String toString() {
        StringBuilder sb= new StringBuilder();
        sb.append("【errorCode】:").append(this.errorCode).append("【msg】:").append(this.msg)
                .append("【inner Caluse】:").append(null == throwable?"Inner Business Caluse":throwable.toString());
        return sb.toString();
    }
}
